package com.lkd.handler;
import com.google.common.collect.Lists;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskDetailsViewModel;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;


@Component
@Slf4j
@Topic(TopicConfig.TASK_SUPPLY_TOPIC)
public class SupplyTaskHandler implements MsgHandler {

    @Autowired
    private TaskService taskService;

    @Autowired
    private VMService vmService;

    /**
     * 创建自动补货工单
     *
     * @param jsonMsg
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {
        log.info("创建自动补货工单");
        SupplyContract supplyContract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);
        if (supplyContract == null || StringUtils.isBlank(supplyContract.getInnerCode()) || CollectionUtils.isEmpty(supplyContract.getSupplyData())) {
            return;
        }
        try {
            VmVO vmInfo = vmService.getVMInfo(supplyContract.getInnerCode());
            if (vmInfo == null) return;
            int userId = taskService.getLastUser(vmInfo.getRegionId(), true);
            if (userId != 0) {
                TaskViewModel taskViewModel = new TaskViewModel();
                taskViewModel.setCreateType(0);
                taskViewModel.setInnerCode(supplyContract.getInnerCode());
                taskViewModel.setUserId(userId);
                taskViewModel.setAssignorId(0);
                taskViewModel.setProductType(VMSystem.TASK_TYPE_SUPPLY);
                taskViewModel.setDesc("创建自动补货工单");
                List<TaskDetailsViewModel> taskDetailsViewModelList = supplyContract.getSupplyData().stream().map(supplyChannel -> {
                    TaskDetailsViewModel taskDetailsViewModel = new TaskDetailsViewModel();
                    taskDetailsViewModel.setChannelCode(supplyChannel.getChannelId());
                    taskDetailsViewModel.setExpectCapacity(supplyChannel.getCapacity());
                    taskDetailsViewModel.setSkuId(supplyChannel.getSkuId());
                    taskDetailsViewModel.setSkuName(supplyChannel.getSkuName());
                    taskDetailsViewModel.setSkuImage(supplyChannel.getSkuImage());
                    return taskDetailsViewModel;
                }).collect(Collectors.toList());
                taskViewModel.setDetails(taskDetailsViewModelList);
                taskService.createTask(taskViewModel);
            }
        } catch (LogicException e) {
            e.printStackTrace();
            log.error("创建自动补货工单失败，接收的信息：", jsonMsg);
        }
    }
}
